//
//  DrawViewController.m
//  Leisure
//
//  Created by Alex on 16/6/28.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "DrawViewController.h"

@interface DrawViewController ()<MenuViewControllerDelegate>
// 接收菜单栏
@property (nonatomic,strong)MenuViewController *menuVC;
// 右侧视图导航栏控制器
@property (nonatomic,strong)UINavigationController *rightNVC;

@end

@implementation DrawViewController

// 自定义初始化方法，在初始化的时候，就直接给上左右控制器

- (instancetype)initWithMenuViewController:(MenuViewController *)menuVC rightViewController:(RightViewController *)rightVC
{
    self = [super init];
    if (self) {
        self.menuVC = menuVC;
        self.menuVC.delegate = self;
        self.rightNVC = [[UINavigationController alloc]initWithRootViewController:rightVC];
        // 在init方法里不能调用添加子视图
        //[self.view addSubview:self.menuVC];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加子视图控制器
    [self addChildViewController:self.menuVC];
    [self addChildViewController:self.rightNVC];
    
    // 添加子视图
    [self.view addSubview:self.menuVC.view];
    [self.view addSubview:self.rightNVC.view];
    
    
    UIScreenEdgePanGestureRecognizer *Edge = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgeAction:)];
    
    Edge.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:Edge];
    
}


// 实现协议方法更换右视图
- (void)menuViewControllerDidClickCellWithVC:(RightViewController *)vc {
    
    [UIView animateWithDuration:0.2 animations:^{
       // 替换之前先把旧的向右偏移
        self.rightNVC.view.frame = CGRectMake(kWidth, 0, kWidth, kHeight);
        
    } completion:^(BOOL finished) {
        
        // 先移除旧的
        [self.rightNVC removeFromParentViewController];
        [self.rightNVC.view removeFromSuperview];
        // 添加新的视图
        // 只能重新初始化，不能直接更换rootViewController
        self.rightNVC = [[UINavigationController alloc]initWithRootViewController:vc];
        [self addChildViewController:self.rightNVC];
        [self.view addSubview:self.rightNVC.view];
        
        self.rightNVC.view.frame = CGRectMake(kWidth, 0, kWidth, kHeight);
        
        [UIView animateWithDuration:0.2 delay:0.2 options:UIViewAnimationOptionLayoutSubviews animations:^{
            
            // 把新的再偏移回来
            self.rightNVC.view.frame = CGRectMake(0, 0, kWidth, kHeight);
            
        } completion:^(BOOL finished) {
            
        }];
        
    }];

    
}

- (void)edgeAction:(UIScreenEdgePanGestureRecognizer *)sender {
    
    if (UIGestureRecognizerStateBegan == sender.state || UIGestureRecognizerStateChanged == sender.state) {
        CGPoint translation = [sender translationInView:sender.view];
        self.rightNVC.view.center = CGPointMake(self.view.frame.size.width /2 +translation.x, self.view.frame.size.height/2);
    }else {
        [UIView animateWithDuration:.3 animations:^{
            self.rightNVC.view.center = CGPointMake(self.view.frame.size.width /2 , self.view.frame.size.height /2 );
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
